
package com.jf.cloud.order.controller;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.order.dto.PurchaseOrderItemDTO;
import com.jf.cloud.order.model.PurchaseOrderItem;
import com.jf.cloud.order.service.PurchaseOrderItemService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 采购商品
 *
 * @author zz
 * @date 2022-06-22 17:12:02
 */
@RestController("multishopPurchaseOrderItemController")
@RequestMapping("/m/purchase_prod")
@Tag(name = "采购商品")
public class PurchaseOrderItemController {

    @Autowired
    private PurchaseOrderItemService purchaseOrderItemService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "获取采购商品列表" , description = "分页获取采购商品列表")
    public ServerResponseEntity<PageVO<PurchaseOrderItem>> page(@Valid @ParameterObject PageDTO pageDTO) {
        PageVO<PurchaseOrderItem> purchaseOrderItemPage = purchaseOrderItemService.page(pageDTO);
        return ServerResponseEntity.success(purchaseOrderItemPage);
    }

    @GetMapping
    @Operation(summary = "获取采购商品" , description = "根据purchaseSpuId获取采购商品")
    public ServerResponseEntity<PurchaseOrderItem> getByPurchaseSpuId(@ParameterObject @RequestParam Long purchaseSpuId) {
        return ServerResponseEntity.success(purchaseOrderItemService.getByPurchaseSpuId(purchaseSpuId));
    }

    @PostMapping
    @Operation(summary = "保存采购商品" , description = "保存采购商品")
    public ServerResponseEntity<Void> save(@Valid @RequestBody PurchaseOrderItemDTO purchaseOrderItemDTO) {
        PurchaseOrderItem purchaseOrderItem = mapperFacade.map(purchaseOrderItemDTO, PurchaseOrderItem.class);
        purchaseOrderItem.setPurchaseItemId(null);
        purchaseOrderItemService.save(purchaseOrderItem);
        return ServerResponseEntity.success();
    }
}
